import pathToRegexp from 'path-to-regexp';
import { getHots } from './api';
import { Toast } from 'antd-mobile';

export default {
    namespace: 'rank',
    state: {
        list:[],
        storeId:""
    },
    reducers: {
        save(state, { data }) {
            return { ...state, ...data };
        },

        saveList(state, { data }) {

            if( data.pageNo !==undefined && data.pageNo == 1 ){

                return { ...state, ...data, hasMore: data.list.length == data.pageSize };
                //  yield put({ type: 'save', data: { ...result.ret, ...obj, hasMore: result.ret.list.length == result.pageSize } });
            }else {
                return { ...state, ...data, hasMore: data.list.length == data.pageSize, list:[...state.list, data.list ]  };
            }

        },


    },
    effects: {
        *getHotsEFF({ obj }, { put, call, select }) {
            //Toast.loading();
          //  const evaluateList = yield select((state)=>state.evaluateList);
            const result = yield call(getHots, {  ...obj });
            if (result.code == 200) {

                yield put({ type: 'save', data: { ...result.ret} });

            //    return { r:true };

            } else {

            }
        },


        *couponlistEFF({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(couponlistApi, obj);
            if (result.result == 1) {
                Toast.info(result.success, 1.5);
                yield put({ type: 'save', data: { couponList: result.data || [] } });
            } else {

            }
        },
        *receiveCouponEFF({ obj }, { put, call }) {
            Toast.loading('加载中...', 3);
            const result = yield call(receiveCouponApi, obj);
            if (result.result == 1) {
                Toast.info(result.success, 1.5);
                //yield put({ type:'save', data:{ couponList:result.data||[] }})
            } else {

            }
        },

        *getSpecEFF({ obj }, { put, call }) {
            // Toast.loading('加载中...', 3);
            const result = yield call(getSpec, obj);
            if (result.result == 1) {
                Toast.info(result.success, 1.5);
                //yield put({ type:'save', data:{ couponList:result.data||[] }})
            } else {

            }
        },

        *addCartOrBuyEFF({ obj }, { put, call, select }) {

            const goodsDetail = yield select((state)=>state.goodsDetail);
            const result = yield call(addCartOrBuy, {  goodsId:goodsDetail.goodsDetailInfo.id, count:goodsDetail.buyCount, specId:goodsDetail.goodsDetailInfo.spec.specId , ...obj });
            if (result.code == 200) {
                Toast.success(result.success, 1.5);
                //yield put({ type:'save', data:{ couponList:result.data||[] }})
            } else {

            }
        },

    },
    subscriptions: {
        setup({ dispatch, history }) {
            return history.listen(({ pathname, query }) => {
                //console.log(match)
                if(pathname.includes('/rank/')){
                       const match = pathToRegexp('/rank/:storeId').exec(pathname);
                       dispatch({ type:'getHotsEFF', obj:{ storeId:match[1] } });
                }

                if (pathname.includes('/goodsDetail')) {
                    // const match = pathToRegexp('/rank/:storeId').exec(pathname);
                    dispatch({ type:'getHotsEFF', obj:{ storeId: ""} });
                }
            });
        },
    },
};
